package {
		
	import org.tuio.tuio.*;
	import flash.display.*;
	import flash.text.*;
	import flash.ui.*;
	import flash.events.*;
	
	public class Main extends MovieClip implements ITuioListener {
		
		public var currentRing:Ring;
		public var tf:TextField;
		public var ringSize:Number;
		
		public function Main(){
			
			this.ringSize = 10;
			
			var tuio:TuioClient = new TuioClient(TuioClient.CONNECTION_MODE_TCP);
			tuio.addListener(this);
			
			tf = new TextField();
			tf.width = 400;
			tf.autoSize = TextFieldAutoSize.LEFT;
			stage.addChild(tf);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
		}
		
		public function reportKeyDown(event:KeyboardEvent):void { 	
			if(event.keyCode == Keyboard.UP){
				tf.y+=10;
			} else if (event.keyCode == Keyboard.DOWN){
				tf.y-=10;
			} else if (event.keyCode == Keyboard.SPACE){
				tf.visible = !tf.visible;
			} else if (event.keyCode == Keyboard.LEFT){
				this.ringSize -= 2;
				if(tf.visible) tf.appendText("\n new ringsize - "+this.ringSize);
			} else if (event.keyCode == Keyboard.RIGHT){
				this.ringSize += 2;
				if(tf.visible) tf.appendText("\n new ringsize - "+this.ringSize);
			}
			
		} 
		
		public function addTuioObject(tuioObject:TuioObject):void {
			tf.appendText("\n add Object - "+tuioObject.toString());
			new Ring(tuioObject.sessionID.toString(), stage, tuioObject.x*stage.stageWidth, tuioObject.y * stage.stageHeight, this.ringSize, 0x33ee33);
		}
		
		public function updateTuioObject(tuioObject:TuioObject):void {
			if(tf.visible) tf.appendText("\n update Object - "+tuioObject.toString());
			currentRing = stage.getChildByName(tuioObject.sessionID.toString()) as Ring;
			currentRing.x = tuioObject.x*stage.stageWidth;
			currentRing.y = tuioObject.y*stage.stageHeight;
		}
		
		public function removeTuioObject(tuioObject:TuioObject):void {
			if(tf.visible) tf.appendText("\n remove Object"+tuioObject.toString());
			currentRing = stage.getChildByName(tuioObject.sessionID.toString()) as Ring;
			currentRing.finish();
		}


		public function addTuioCursor(tuioCursor:TuioCursor):void {
			tf.appendText("\n add Cursor - "+tuioCursor.toString());
			new Ring(tuioCursor.sessionID.toString(), stage, tuioCursor.x*stage.stageWidth, tuioCursor.y * stage.stageHeight, this.ringSize, 0xee3333);
		}
		
		public function updateTuioCursor(tuioCursor:TuioCursor):void {
			if(tf.visible) tf.appendText("\n update Cursor - "+tuioCursor.toString());
			currentRing = stage.getChildByName(tuioCursor.sessionID.toString()) as Ring;
			currentRing.x = tuioCursor.x*stage.stageWidth;
			currentRing.y = tuioCursor.y*stage.stageHeight;
		}
		
		public function removeTuioCursor(tuioCursor:TuioCursor):void {
			if(tf.visible) tf.appendText("\n remove Cursor"+tuioCursor.toString());
			currentRing = stage.getChildByName(tuioCursor.sessionID.toString()) as Ring;
			currentRing.finish();
		}
		
		
		public function addTuioBlob(tuioBlob:TuioBlob):void {
			tf.appendText("\n add Blob - "+tuioBlob.toString());
			new Ring(tuioBlob.sessionID.toString(), stage, tuioBlob.x*stage.stageWidth, tuioBlob.y * stage.stageHeight, this.ringSize, 0x3333ee);
		}
		
		public function updateTuioBlob(tuioBlob:TuioBlob):void {
			if(tf.visible) tf.appendText("\n update Blob - "+tuioBlob.toString());
			currentRing = stage.getChildByName(tuioBlob.sessionID.toString()) as Ring;
			currentRing.x = tuioBlob.x*stage.stageWidth;
			currentRing.y = tuioBlob.y*stage.stageHeight;
		}
		
		public function removeTuioBlob(tuioBlob:TuioBlob):void {
			if(tf.visible) tf.appendText("\n remove Blob"+tuioBlob.toString());
			currentRing = stage.getChildByName(tuioBlob.sessionID.toString()) as Ring;
			currentRing.finish();
		}
		
	}
}

import flash.display.*;

internal class Ring extends Sprite {
		
		public function Ring(name:String, cont:DisplayObjectContainer, x:int, y:int, radius:int, color:uint){
			this.blendMode = BlendMode.DARKEN;
			this.graphics.lineStyle(1, color, 1, false, LineScaleMode.NONE);
			this.graphics.beginFill(color, 0.5);
			this.graphics.drawCircle(0,0,radius);
			this.graphics.endFill();
			this.x = x;
			this.y = y;
			this.name = name;

			cont.addChild(this);
		}
		
		public function finish():void{
			this.parent.removeChild(this);
		}
		
	}
